package com.hz.qs.service;

import cn.hutool.crypto.SecureUtil;
import com.hz.qs.dao.UserInfoDao;
import com.hz.qs.model.UserInfo;
import com.hz.qs.utils.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class HealthMasterService {

    @Autowired
    private UserInfoDao userInfoDao;

    /**
     * 18健管师管理页面
     *
     * @param
     * @return com.hz.qs.utils.RestResult
     * @author yejy
     * @date 2020/11/2 22:20
     */
    public RestResult getList() {
        List<UserInfo> infoList = userInfoDao.selectAllData();
        return RestResult.successResult(infoList);
    }

    /**
     * 19，20健管师编辑页面
     *
     * @param userInfo
     * @return com.hz.qs.utils.RestResult
     * @author yejy
     * @date 2020/11/2 22:20
     */
    @Transactional(rollbackFor = Exception.class)
    public RestResult edit(UserInfo userInfo) {
        Optional.ofNullable(userInfo.getPassword()).ifPresent(p -> {
            String password = userInfo.getPassword();
            String md5 = SecureUtil.md5(password);
            userInfo.setPassword(md5);
        });
        int update = userInfoDao.edit(userInfo);
        return RestResult.successResult(update);
    }
}
